<html>
<head><meta charset="utf-8"><title>Attribute removal on macro-expanded items · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Attribute.20removal.20on.20macro-expanded.20items.html">Attribute removal on macro-expanded items</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="233684493"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Attribute%20removal%20on%20macro-expanded%20items/near/233684493" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Attribute.20removal.20on.20macro-expanded.20items.html#233684493">(Apr 08 2021 at 16:30)</a>:</h4>
<p>I'm looking for the code in rustc that strips <code>#[derive]</code> attributes from items before they're fed into custom derives. I've found <a href="https://github.com/rust-lang/rust/blob/69e1d22ddbc67b25141a735a22a8895a678b32ca/compiler/rustc_expand/src/expand.rs#L1029">the code</a> that does this for attribute macros already, but <code>#[derive]</code> seems to go through another code path. Any pointers?</p>



<a name="233693325"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Attribute%20removal%20on%20macro-expanded%20items/near/233693325" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Attribute.20removal.20on.20macro-expanded.20items.html#233693325">(Apr 08 2021 at 17:21)</a>:</h4>
<p><code>#[derive]</code> is a regular macro attribute after <a href="https://github.com/rust-lang/rust/pull/79078">https://github.com/rust-lang/rust/pull/79078</a>, so it's the same linked code as for other attributes.</p>



<a name="233693990"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Attribute%20removal%20on%20macro-expanded%20items/near/233693990" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Attribute.20removal.20on.20macro-expanded.20items.html#233693990">(Apr 08 2021 at 17:25)</a>:</h4>
<p>Ah, okay</p>



<a name="233693995"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Attribute%20removal%20on%20macro-expanded%20items/near/233693995" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Attribute.20removal.20on.20macro-expanded.20items.html#233693995">(Apr 08 2021 at 17:25)</a>:</h4>
<p>Thanks!</p>



<a name="233748290"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Attribute%20removal%20on%20macro-expanded%20items/near/233748290" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Attribute.20removal.20on.20macro-expanded.20items.html#233748290">(Apr 08 2021 at 23:58)</a>:</h4>
<p>It seems that custom derives are unable to see later <code>#[derive]</code> attributes, but the code I linked shouldn't be removing those. Are they aggregated somewhere first, or how does this work?</p>



<a name="233750658"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Attribute%20removal%20on%20macro-expanded%20items/near/233750658" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Attribute.20removal.20on.20macro-expanded.20items.html#233750658">(Apr 09 2021 at 00:26)</a>:</h4>
<p>I just tested it locally - it looks like derives are getting expanded in the wrong order</p>



<a name="233750757"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Attribute%20removal%20on%20macro-expanded%20items/near/233750757" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Attribute.20removal.20on.20macro-expanded.20items.html#233750757">(Apr 09 2021 at 00:27)</a>:</h4>
<p>they're getting expanded bottom-to-top (right to left), instead of left-to-right</p>



<a name="233750781"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Attribute%20removal%20on%20macro-expanded%20items/near/233750781" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Attribute.20removal.20on.20macro-expanded.20items.html#233750781">(Apr 09 2021 at 00:27)</a>:</h4>
<p>but each derive gets invoked with the proper input (e.g. the leftmost derive sees the other derives in the input, but it just invoked after them)</p>



<a name="233750842"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Attribute%20removal%20on%20macro-expanded%20items/near/233750842" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Attribute.20removal.20on.20macro-expanded.20items.html#233750842">(Apr 09 2021 at 00:28)</a>:</h4>
<p>derives can't modify the item, so this has probably gone unnoticed for a while</p>



<a name="233750931"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Attribute%20removal%20on%20macro-expanded%20items/near/233750931" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Attribute.20removal.20on.20macro-expanded.20items.html#233750931">(Apr 09 2021 at 00:30)</a>:</h4>
<p>Here's the problem: <a href="https://github.com/rust-lang/rust/blob/2e495d2e845cf27740e3665f718acfd3aa17253e/compiler/rustc_expand/src/expand.rs#L516-L542">https://github.com/rust-lang/rust/blob/2e495d2e845cf27740e3665f718acfd3aa17253e/compiler/rustc_expand/src/expand.rs#L516-L542</a></p>



<a name="233750993"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Attribute%20removal%20on%20macro-expanded%20items/near/233750993" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Attribute.20removal.20on.20macro-expanded.20items.html#233750993">(Apr 09 2021 at 00:30)</a>:</h4>
<p>we push derives in-order, but we then pop off invocations in reverse order</p>



<a name="233756369"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Attribute%20removal%20on%20macro-expanded%20items/near/233756369" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Attribute.20removal.20on.20macro-expanded.20items.html#233756369">(Apr 09 2021 at 01:47)</a>:</h4>
<p>Opened <a href="https://github.com/rust-lang/rust/pull/84023">https://github.com/rust-lang/rust/pull/84023</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>